package com.mcintyret.site.service.journal;

import com.mcintyret.site.context.UserContext;
import com.mcintyret.site.journal.JournalFamily;
import com.mcintyret.site.journal.database.JournalFamilyDao;
import com.mcintyret.site.web.controller.journal.forms.JournalFamilyForm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * User: mcintyret2
 * Date: 15/04/2012
 */
@Service
public class JournalFamilyHelper {

    @Resource
    private JournalFamilyDao journalFamilyDao;
    @Resource
    private UserContext userContext;

    public Integer addNewJournalFamily(JournalFamilyForm journalFamilyForm) {
        JournalFamily newJournalFamily = createJournalFamilyFromForm(journalFamilyForm);
        return journalFamilyDao.addAndGetId(newJournalFamily);
    }

    private JournalFamily createJournalFamilyFromForm(JournalFamilyForm form) {
        String username = userContext.getUsername();
        int order = journalFamilyDao.getCountForCurrentUser();
        return new JournalFamily(order, username, form.getName(), form.getDescription());
    }

    public void deleteJournalFamilyWithId(int journalFamilyId) {
        journalFamilyDao.delete(journalFamilyDao.get(journalFamilyId));
    }
}
